package com.powerhua.privilege.web.action;

import com.powerhua.core.utils.CommonUtils;
import java.util.List;

import javax.servlet.ServletRequest;

import com.powerhua.privilege.PrivilegeUtils;
import com.powerhua.privilege.domain.User;
import com.powerhua.privilege.web.action.convert.UserConverter;
import com.powerhua.support.web.HtmlBeanConverter;

public class UserAction implements ActionInterface {

    public String doAction(ServletRequest req) {
        // result
        String result = "";

        // action
        String action = CommonUtils.getStringFromReq(req, "a");
        
        if (action.equals("getUserInfo")) {
            User user = (User) HtmlBeanConverter.getBeanFromHtml(User.class, req);
            User userFromDb = getUserInfo(user);
            if (userFromDb == null) {
                result = "";
            } else {
                result = UserConverter.getInfoScriptStr(userFromDb);
            }
        } else if (action.equals("getAllUserById")) {
            String start = CommonUtils.getStringFromReq(req, "start");
            String offset = CommonUtils.getStringFromReq(req, "offset");
            List list = getList(start, offset);
            result = UserConverter.getListScriptStr(list);
        } else if(action.equals("userLogin")) {
        	User user = (User) HtmlBeanConverter.getBeanFromHtml(User.class, req);
        	User userFormDb = checkUserLogin(user);
        }

        // return
        return result;
    }
    
    

    private User getUserInfo(User user) {
        List list = PrivilegeUtils.getUserManager().findUsers(user, 0, 0);
        if (!(list.size() == 1)) {
            return null;
        } else {
            return (User) list.get(0);
        }
    }

    private List getList(String start, String offset) {
        int intStart = Integer.parseInt(start);
        int intOffset = Integer.parseInt(offset);
        List list = PrivilegeUtils.getUserManager().findUsers(null, intStart, intOffset);
        return list;
    }
    
    private User checkUserLogin(User user) {
    	return user;
    }

}
